package com.guaoran.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;


/**
 * @author : 孤傲然
 * @title : RedisConfig 
 * @Description : 
 * 	 配置集群
 * @date :2018/1/25 22:34
 */
//@Configuration
public class RedisConfig {
	@Value("${spring.redis.cluster.nodes}")
	private String clusterNodes;
	
	@Bean
	public JedisCluster getJedisCluster(){
		//分隔集群节点
		String[] nodes = clusterNodes.split(",");
		//创建set集合对象
		Set<HostAndPort> hostAndPorts = new HashSet<HostAndPort>();
		//循环集群节点对象
		for (String node : nodes) {
			String[] hp = node.split(":");
			hostAndPorts.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
		}
		//创建Redis集群对象
		JedisCluster jedisCluster = new JedisCluster(hostAndPorts);
		return jedisCluster;
	}
}
